package com.gi.profiler.db;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.gi.profiler.model.ConnectivitySettings;
import com.gi.profiler.model.DayEnum;
import com.gi.profiler.model.DisplaySettings;
import com.gi.profiler.model.Profile;
import com.gi.profiler.model.ProfileCategory;
import com.gi.profiler.model.MediaFile;
import com.gi.profiler.model.Spot;
import com.gi.profiler.model.SpotProfileTime;
import com.gi.profiler.model.SoundSettings;
import com.gi.profiler.model.VolumeSettings;
import com.gi.profiler.model.Wallpaper;
import com.gi.profiler.model.WayPoint;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    private static final String DATABASE_NAME = "locationprofiler.db";
    private static final int DATABASE_VERSION = 1;
    private final String LOG_NAME = getClass().getName();

    private Dao<ConnectivitySettings, Integer> connSettingsDao;
    private Dao<DisplaySettings, Integer> displaySettingsDao;
    private Dao<Profile, Integer> profileDao;
    private Dao<ProfileCategory, Integer> profileCategoryDao;
    private Dao<MediaFile, Integer> ringtoneDao;
    private Dao<Spot, Integer> spotDao;
    private Dao<SpotProfileTime, Integer> spotProfileTimeDao;
    private Dao<SoundSettings, Integer> soundSettingsDao;
    private Dao<VolumeSettings, Integer> volumeSettingsDao;
    private Dao<Wallpaper, Integer> wallpaperDao;
    private Dao<WayPoint, Integer> wayPointDao;
    private Dao<DayEnum, Integer> weekDayDao;

    public DatabaseHelper(Context context) {
	super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
	try {
	    TableUtils.createTable(connectionSource, ConnectivitySettings.class);
	    TableUtils.createTable(connectionSource, DisplaySettings.class);
	    TableUtils.createTable(connectionSource, Profile.class);
	    TableUtils.createTable(connectionSource, ProfileCategory.class);
	    TableUtils.createTable(connectionSource, MediaFile.class);
	    TableUtils.createTable(connectionSource, Spot.class);
	    TableUtils.createTable(connectionSource, SpotProfileTime.class);
	    TableUtils.createTable(connectionSource, SoundSettings.class);
	    TableUtils.createTable(connectionSource, VolumeSettings.class);
	    TableUtils.createTable(connectionSource, Wallpaper.class);
	    TableUtils.createTable(connectionSource, WayPoint.class);

	} catch (SQLException e) {
	    Log.e(LOG_NAME, "Unable to create database", e);
	}

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVer,
	    int newVer) {
	try {
	    Log.i(LOG_NAME, "onUpgrade");
	    TableUtils.dropTable(connectionSource, ConnectivitySettings.class, true);
	    TableUtils.dropTable(connectionSource, DisplaySettings.class, true);
	    TableUtils.dropTable(connectionSource, Profile.class, true);
	    TableUtils.dropTable(connectionSource, ProfileCategory.class, true);
	    TableUtils.dropTable(connectionSource, MediaFile.class, true);
	    TableUtils.dropTable(connectionSource, Spot.class, true);
	    TableUtils.dropTable(connectionSource, SpotProfileTime.class, true);
	    TableUtils.dropTable(connectionSource, SoundSettings.class, true);
	    TableUtils.dropTable(connectionSource, VolumeSettings.class, true);
	    TableUtils.dropTable(connectionSource, Wallpaper.class, true);
	    TableUtils.dropTable(connectionSource, WayPoint.class, true);
	    onCreate(db, connectionSource);
	} catch (SQLException e) {
	    Log.e(DatabaseHelper.class.getName(), "Unable to upgrade database from version "
		    + oldVer + " to ver  " + newVer, e);
	}
    }

    public Dao<ConnectivitySettings, Integer> getConnSettingsDao() throws SQLException {
	if (connSettingsDao == null) {
	    connSettingsDao = getDao(ConnectivitySettings.class);
	}
	return connSettingsDao;
    }
    
    public Dao<DisplaySettings, Integer> getDisplayDao() throws SQLException {
   	if (displaySettingsDao == null) {
   	    displaySettingsDao = getDao(DisplaySettings.class);
   	}
   	return displaySettingsDao;
       }

    public Dao<Profile, Integer> getProfileDao() throws SQLException {
	if (profileDao == null) {
	    profileDao = getDao(Profile.class);
	}
	return profileDao;
    }

    public Dao<ProfileCategory, Integer> getProfileCategoryDao() throws SQLException {
	if (profileCategoryDao == null) {
	    profileCategoryDao = getDao(ProfileCategory.class);
	}
	return profileCategoryDao;
    }

    public Dao<MediaFile, Integer> getRingtoneDao() throws SQLException {
	if (ringtoneDao == null) {
	    ringtoneDao = getDao(MediaFile.class);
	}
	return ringtoneDao;
    }

    public Dao<Spot, Integer> getSpotDao() throws SQLException {
	if (spotDao == null) {
	    spotDao = getDao(Spot.class);
	}
	return spotDao;
    }

    public Dao<SpotProfileTime, Integer> getSpotProfileTimeDao() throws SQLException {
	if (spotProfileTimeDao == null) {
	    spotProfileTimeDao = getDao(SpotProfileTime.class);
	}
	return spotProfileTimeDao;
    }


    public Dao<SoundSettings, Integer> getSoundSettingsDao() throws SQLException {
	if (soundSettingsDao == null) {
	    soundSettingsDao = getDao(SoundSettings.class);
	}
	return soundSettingsDao;
    }
    
    public Dao<VolumeSettings, Integer> getVolumeSettingsDao() throws SQLException {
  	if (volumeSettingsDao == null) {
  	  volumeSettingsDao = getDao(VolumeSettings.class);
  	}
  	return volumeSettingsDao;
      }

    public Dao<Wallpaper, Integer> getWallpaperDao() throws SQLException {
	if (wallpaperDao == null) {
	    wallpaperDao = getDao(Wallpaper.class);
	}
	return wallpaperDao;
    }

    public Dao<WayPoint, Integer> getWayPointDao() throws SQLException {
	if (wayPointDao == null) {
	    wayPointDao = getDao(WayPoint.class);
	}
	return wayPointDao;
    }

    public Dao<DayEnum, Integer> getWeekDayDao() throws SQLException {
	if (weekDayDao == null) {
	    weekDayDao = getDao(DayEnum.class);
	}
	return weekDayDao;
    }
    

    /**
     * Close the database connections and clear any cached DAOs.
     */
    @Override
    public void close() {
	super.close();
	connSettingsDao = null;
	displaySettingsDao = null;
	profileDao = null;
	profileCategoryDao = null;
	ringtoneDao = null;
	spotDao = null;
	spotProfileTimeDao = null;
	soundSettingsDao = null;
	wallpaperDao = null;
	wayPointDao = null;
	weekDayDao = null;
    }

}
